﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class MagerMqLogController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        // GET: Mager_Department
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, iot_mq_log reqobj, iot_device reqdevice, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            ViewData["reqobj"] = reqobj;
            ViewData["reqdevice"] = reqdevice;
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " ";
            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and d_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));

            }
            if (!string.IsNullOrEmpty(reqdevice.d_code))
            {
                sqlwhere = sqlwhere + " and d_code=@d_code ";
                sugarParameters.Add(new SugarParameter("d_code", reqdevice.d_code));

            }
            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            if (!string.IsNullOrWhiteSpace(reqobj.ml_gw_d_guid))
            {
                sqlwhere = sqlwhere + " and ml_gw_d_guid=@ml_gw_d_guid";
                sugarParameters.Add(new SugarParameter("ml_gw_d_guid", reqobj.ml_gw_d_guid));
            }
            //  sqlwhere = sqlwhere + "  and ml_id>IFNULL((select ml_id from iot_mq_log order by ml_id desc limit 10000,1) ,0) ";
            string sql = "select  a.*,m_name,ml_content,ml_type,ml_mqevent,ml_remark,ml_result,CAST(ml_add_time AS CHAR) ml_add_time,ml_id  from iot_device a  inner join iot_device_model on m_guid=d_model_guid inner join iot_mq_log on ml_device_guid=d_guid  " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, " ml_id desc", ref totalRecord);

            //  iot_mq_log log=ef.iot_mq_log.OrderByDescending(c=>c.ml_id).FirstOrDefault();

            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码
            return View(httpRequest);
        }
        public IActionResult ListDialog(string type)
        {
            ViewBag.Type = type;
            return View();
        }

        public RespResultData getGateWayList(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDevice().getDeviceGateWayAllList());
        }


    }
}